<?php
namespace Admin\Controller;

class IndexController extends Controller {

    public function index() {
        if(0) {
			if($aderMoney = M('vAder')->select()) {
				$this->assign('aderMoney', $aderMoney);
			}

        /**
         * 服务器列表
         */
        if ($servers = D2('Server')->order('id DESC')->select()) {

            /**
             * 计费服务器列表
             */
            if ($costServers = D2('CostServer')->select()) {
                foreach ($servers as $k => $v) {
                    foreach ($costServers as $k2 => $v2) {
                        $_status = ($v2['slave_ip'] == $v['ip']) ? 1 : 0;
                        $servers[$k]['is_cost_server'] = $_status; 

                        if ($_status) {
                            break;
                        }
                    }
                }
            }

            $this->assign('servers', $servers);
        }
        }
        $this->display();
    }

    public function login() {
        $this->display();
    }

    public function doLogin() {
        $name   = I('post.name', '');
        $passwd = I('post.passwd', '');
        $msg    = array();

        if(empty($name)) {
            $msg[] = '请输入登陆名称';
        }

        if(empty($passwd)) {
            $msg[] = '请输入密码';
        }

        if(!empty($msg)) {
            showJson(array('s' => false, 'm' => implode("\n", $msg)));
        }
        
        $mUser = D2('User');
        $error = '';

        try {
            $rUser = $mUser->getLogin($name, $passwd);
            $mUser->setLoginAppName(MODULE_NAME);
            $mUser->setLoginUser($rUser, MODULE_NAME);
        } catch(\Exception $e) {
            $error = $e->getMessage();
        }

        $r = empty($error) ? array('s' => true, 'm' => '登陆成功') : array('s' => false, 'm' => $error);

        showJson($r);
    }

    public function doLogout() {
        $mUser = D2('User');
        $mUser->logout();

        showJson(array('s' => true, 'm' => '退出成功'));
    }

    public function doPasswd() {
        $oldPasswd = I('post.old_passwd', '');
        $newPasswd = I('post.new_passwd', '');
        $msg       = array();

        if(empty($oldPasswd)) {
            $msg[] = '请输入旧密码';
        }

        if(empty($newPasswd)) {
            $msg[] = '请输入新密码';
        }

        if(empty($msg) && $oldPasswd == $newPasswd) {
            $msg[] = '新密码不同与老密码相同';
        }

        if(!empty($msg)) {
            showJson(array('s' => false, 'm' => implode("\n", $msg)));
        }

        $mUser = D2('User');
        $rUser = $mUser->getLoginUser();

        if(!$rUser = $mUser->getByUid($rUser['uid'])) {
            showJson(array('s' => false, 'm' => '没有找到会员信息'));
        }

        if($rUser['password'] != md5($oldPasswd)) {
            showJson(array('s' => false, 'm' => '旧密码有误'));
        }

        $r = $mUser->where(array('uid' => $rUser['uid']))->setField('password', md5($newPasswd))
             ? array('s' => true, 'm' => '密码修改成功')
             : array('s' => false, 'm' => '密码修改出错');

        showJson($r);
    }

    /**
     * getRunningCostServer
     * 获取正在运行中的计费服务器ip
     *
     * @return void
     */
    public function getRunningCostServer() {
        $ip = '';

        if ($r = D2('CostServer')->where(array('status' => 1))->find()) {
            $ip = $r['slave_ip'];
        }

        showJson(array('s' => true, 'd' => $ip));
    }

}
